#!/bin/bash -e
source reporting.sh

RESULT_LINES=/tmp/result_lines.txt
MONO_LOG=/tmp/mono-log.txt
MANIFEST=./module_manifest.json
REPORT_FILE=/tmp/report.txt
status=pass

echo Baseline ping test report | tee -a $MONO_LOG
# This is a check that the startup.pcap file exists and is proper.
tcpdump -en -r /scans/startup.pcap ip

echo %% $(tcpdump -n -r /scans/startup.pcap | wc -l) packets captured. | tee -a $MONO_LOG
echo

STATIC_IP=`jq -r .static_ip /config/device/module_config.json`
if [ "$STATIC_IP" == null ]; then
    STATIC_IP=
fi

if [ -n "$STATIC_IP" ]; then
    echo RESULT skip base.startup.dhcp Using static ip $STATIC_IP | tee -a $RESULT_LINES
else
    req=pass
    ack=pass
    echo
    tcpdump -ven -r /scans/startup.pcap port 67 | fgrep "length 1: Request" || req=fail
    tcpdump -ven -r /scans/startup.pcap port 67 | fgrep "length 1: ACK" || ack=fail1
    if [ $req = "fail" ] || [ $ack = "fail" ]; then
        status=fail
        echo RESULT fail base.startup.dhcp missing dhcp packets| tee -a $RESULT_LINES
    else
        echo RESULT pass base.startup.dhcp | tee -a $RESULT_LINES
    fi
fi

# Setup for accessing control plane switch. If LOCAL_IP is defined, which
# is the intended local address for this node on the control plane then
# SWITCH_IP will be the IP address of the OpenFlow switch.
if [ -n "$LOCAL_IP" ]; then
    echo Configuring network with local address $LOCAL_IP | tee -a $MONO_LOG

    LOCAL_ONLY=${LOCAL_IP%/*}
    LOCAL_INDEX=${LOCAL_ONLY##*.}
    echo Using IP index $LOCAL_INDEX
    LOCAL_MAC=ca:50:20:b1:4f:$(printf %02x $LOCAL_INDEX)
    echo Using local MAC address $LOCAL_MAC

    LOCAL_IFACE=`hostname`-eth0
    ip link set dev $LOCAL_IFACE addr $LOCAL_MAC
    ip addr add $LOCAL_IP dev $LOCAL_IFACE

    ping -n -c 2 $SWITCH_IP || true

    # Delete any local arp entry, to force a re-arp
    arp -d $SWITCH_IP || true

    echo Switch test with target $SWITCH_IP:$SWITCH_PORT | tee -a $MONO_LOG
    local_status=pass
    ping -n -c 10 $SWITCH_IP || local_status=fail
    echo RESULT $local_status base.switch.ping target %% $SWITCH_IP:$SWITCH_PORT | tee -a $RESULT_LINES
    if [ $local_status != pass ]; then
        status=fail
    fi
else
    echo LOCAL_IP not configured, assuming no network switch | tee -a $MONO_LOG
    echo RESULT skip base.switch.ping No local IP has been set, check ext_loip in system.conf | tee -a $RESULT_LINES
fi

ping_runtime=/config/port/ping_runtime.sh
if [ -f $ping_runtime ]; then
    echo Executing $ping_runtime
    source $ping_runtime
else
    echo $ping_runtime not found.
fi

# First ping might fail b/c of warm-up delays.
summary=""
local_status=pass
ping -n -c 10 $TARGET_IP || (local_status=fail && status=fail)

if [ $local_status == pass ]; then
    summary="target reached"
else
    summary="could not reach target"
fi

echo RESULT $local_status base.target.ping $summary %% $TARGET_IP | tee -a $RESULT_LINES

echo | tee -a $MONO_LOG
echo Done with basic connectivity tests | tee -a $MONO_LOG

write_out_monolog $REPORT_FILE \
                  $MANIFEST \
                  $MONO_LOG \
                  $RESULT_LINES

echo Critical module status is $status
[ "$status" == pass ]
